package cn.iocoder.yudao.module.system.controller.admin.product;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.system.dal.dataobject.product.ProductRenewalItemDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.product.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.module.system.service.product.ProductService;

@Tag(name = "管理后台 - 系统产品")
@RestController
@RequestMapping("/system/product")
@Validated
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping("/create")
    @Operation(summary = "创建系统产品")
    @PreAuthorize("@ss.hasPermission('system:product:create')")
    public CommonResult<Long> createProduct(@Valid @RequestBody ProductSaveReqVO createReqVO) {
        return success(productService.createProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统产品")
    @PreAuthorize("@ss.hasPermission('system:product:update')")
    public CommonResult<Boolean> updateProduct(@Valid @RequestBody ProductSaveReqVO updateReqVO) {
        productService.updateProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:product:delete')")
    public CommonResult<Boolean> deleteProduct(@RequestParam("id") Long id) {
        productService.deleteProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @OperateLog(enable = false)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ProductRespVO> getProduct(@RequestParam(value = "id") Long id) {

        ProductDO product = productService.getProduct(id);

        if (ObjUtil.isNull(product)) {
            throw exception(new ErrorCode(500, "参数错误"));
        }
        return success(BeanUtils.toBean(product, ProductRespVO.class));
    }

    @GetMapping("/get-by-code")
    @OperateLog(enable = false)
    @Parameter(name = "code", description = "code", required = true, example = "1024")
    public CommonResult<ProductRespVO> getProduct(@RequestParam(value = "code") String code) {
        ProductDO product = productService.getProductByCode(code);
        if (ObjUtil.isNull(product)) {
            throw exception(new ErrorCode(500, "参数错误"));
        }
        return success(BeanUtils.toBean(product, ProductRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得系统产品")
    public CommonResult<List<ProductRespVO>> getProductList() {
        ProductPageReqVO pageReqVO = new ProductPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductDO> list = productService.getProductPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, ProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统产品分页")
    @PreAuthorize("@ss.hasPermission('system:product:query')")
    public CommonResult<PageResult<ProductRespVO>> getProductPage(@Valid ProductPageReqVO pageReqVO) {
        PageResult<ProductDO> pageResult = productService.getProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统产品 Excel")
    @PreAuthorize("@ss.hasPermission('system:product:export')")
    @OperateLog(type = EXPORT)
    public void exportProductExcel(@Valid ProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductDO> list = productService.getProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统产品.xls", "数据", ProductRespVO.class,
                        BeanUtils.toBean(list, ProductRespVO.class));
    }

    @GetMapping("/get-renewal-item-list")
    @Operation(summary = "获得产品续费项")
    @OperateLog(enable = false)
    @Parameter(name = "productId")
    @PreAuthorize("@ss.hasPermission('system:product:update')")
    public CommonResult<List<ProductRenewalItemDO>> getProductRenewalItemList(@RequestParam("productId") Long productId) {
        return success(productService.getRenewalItemList(productId));
    }

    @GetMapping("/get-renewal-item-list-by-code")
    @Operation(summary = "获得产品续费项")
    @OperateLog(enable = false)
    @Parameter(name = "productCode")
    public CommonResult<List<ProductRenewalItemDO>> getProductRenewalItemListByCode(@RequestParam("productCode") String productCode) {
        return success(productService.getRenewalItemList(productCode));
    }

    @PostMapping("/create-renewal-item")
    @Operation(summary = "增加产品续费项")
    @OperateLog(enable = false)
    @PreAuthorize("@ss.hasPermission('system:product:update')")
    public CommonResult<Long> createProductRenewalItem(@RequestBody ProductRenewalItemSaveReqVO saveReqVO) {
        return success(productService.createProductRenewalItem(saveReqVO));
    }

    @PutMapping("/update-renewal-item")
    @Operation(summary = "增加产品续费项")
    @OperateLog(enable = false)
    @PreAuthorize("@ss.hasPermission('system:product:update')")
    public CommonResult<Boolean> updateProductRenewalItem(@RequestBody ProductRenewalItemSaveReqVO saveReqVO) {
        productService.updateProductRenewalItem(saveReqVO);
        return success(true);
    }

    @DeleteMapping("/delete-renewal-item")
    @Operation(summary = "增加产品续费项")
    @OperateLog(enable = false)
    @Parameter(name = "id")
    @PreAuthorize("@ss.hasPermission('system:product:update')")
    public CommonResult<Boolean> updateProductRenewalItem(@RequestParam("id") Long id) {
        productService.deleteProductRenewalItem(id);
        return success(true);
    }

}